package com.syx.extensiondemo.Listener;

import com.rabbitmq.client.Channel;
import com.syx.extensiondemo.Constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Component
public class QosListener {
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void handlerMessage(Message message, Channel channel) throws IOException {
        long deliveryTag= message.getMessageProperties().getDeliveryTag();
        try {
            Thread.sleep(2000);
            System.out.println(message.getMessageProperties().getDeliveryTag()+"222222222222++++++++++++++++++"+new String(message.getBody(), StandardCharsets.UTF_8));
            //如果没有确认，mq就不知道发送对方已经消化了多少消息了
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            channel.basicNack(deliveryTag,false,true);

        }


    }
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void handlerMessage1(Message message, Channel channel) throws IOException {
        long deliveryTag= message.getMessageProperties().getDeliveryTag();
        try {
            Thread.sleep(1000);
            System.out.println(message.getMessageProperties().getDeliveryTag()+"1111111111111++++++++++++++++++"+new String(message.getBody(), StandardCharsets.UTF_8));
            //如果没有确认，mq就不知道发送对方已经消化了多少消息了

            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            channel.basicNack(deliveryTag,false,true);

        }


    }
}
